/**
 * 外部返利维护作业
 * @author miaoyl
 * @date 2019/04/29
 */
define(function () {

    var self,
        doc = document,
        app = win.app,
        layui = win.layui,
        element,
        table,
        table_where = {},
        cols = [[
            {type:'checkbox',fixed: 'left'},
            {field: 'GWDG001_NAME', width: 160, title: '系统外数据类型', fixed: 'left',search:'default'}
            , {field: 'GWDG002', width: 160, title: '日期',sort:true}
            , {field: 'GWDG005_NAME', width: 160, title: '经销商'}
            , {field: 'GWDG004', width: 160, title: '金额',sort:true}
            , {field: 'GWDG003_NAME', width: 160, title: '品类'},
            {
                width: 60,
                title: '',
                toolbar: '#script-gwdc007setting',
                fixed: 'right',
                align: 'center',
                unresize: true
            }
        ]],
        gwdc007SettingAction,
        moduleName = '外部返利',
        editArea = ['600px', '400px']
    ;

    app.newModule({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwdc007',
                _searchs: [{
                    id: 'gwdc007', tableId: 'gwdc007_table', datas: cols[0]
                }],
                _loadTplsFull: [{
                    dom: 'gwdc007_edit', tpl: 'gwdc007_edit'
                }]
            }, win);
        },

        initTable: function () {
            layui.use(['table'], function () {
                table = layui.table;
                table.render({
                    elem: app.getByid(doc, 'gwdc007_table'),
                    url: g_ctxPath + 'crm/gwdc007/getGwdglist',
                    exportsUrl: g_ctxPath + 'crm/gwdc007/getGwdglist',
                    exportsName: moduleName + '数据',
                    title: moduleName + '信息表',
                    method: 'post',
                    id: 'gwdc007_table',
                    toolbar: '#table-toolbar',
                    defaultToolbar: ['filter', 'exports'],
                    height: 'full',
                    cols: cols,
                    loading: true,
                    page: true,
                    where: table_where,
                });

                layui.laytpl(app.getByid(doc, 'script-gwdc007setting-action').innerHTML).render({}, function (string) {
                    gwdc007SettingAction = string;
                });

                var tipsIndex = null;

                table.on('tool(gwdc007_table)', function (obj) {

                    layer.tips(gwdc007SettingAction, obj.td, {
                        tips: [3, '']
                        , time: 3000
                        , anim: 500
                        , maxWidth: 120
                        , skin: 'layui-table-tips'
                        , success: function (layero, index) {
                            layero.find('li').on('click', function () {
                                switch ($(this).attr('inx')) {
                                    case '0':
                                        self._openTplFull({
                                            tag: 'gwdc007_edit',
                                            moduleName:moduleName,
                                            tagData: {
                                                obj:obj
                                            },
                                            index: 0,
                                            openOpts:{
                                                title: moduleName + '信息编辑',
                                                area:editArea
                                            }
                                        });
                                        break;
                                    case '1':
                                        self.delData(obj);
                                        break;
                                }
                                layer.close(index);
                            });
                        }
                    });
                });

                //监听行双击事件
                table.on('rowDouble(gwdc007_table)', function (obj) {
                    self._openTplFull({
                        tag: 'gwdc007_edit',
                        moduleName:moduleName,
                        tagData:  {
                            obj:obj
                        },
                        index: 0,
                        openOpts: {
                            title: moduleName + '信息编辑',
                            area:editArea
                        }
                    });
                });

                //监听事件
                table.on('toolbar(gwdc007_table)', function (obj) {
                    switch (obj.event) {
                        case 'add':
                            self._openTplFull({
                                tag: 'gwdc007_edit',
                                moduleName:moduleName,
                                tagData: {
                                    is_add: true
                                },
                                index: 0,
                                openOpts: {
                                    title: moduleName + '信息编辑',
                                    area:editArea
                                }
                            });
                            break;
                        case 'del':
                            self.delDataBatch();
                            break;
                    }
                });

                $(doc).on('click', function () {
                    tipsIndex && layer.close(tipsIndex);
                });

            });
        },

        reloadTableCurr: function () {
            self._reloadTable({id: 'gwdc007_table', where: table_where});
        },

        delData: function (obj) {
            app.ajax({
                xurl: 'crm/gwdc007/del',
                xdata: obj.data,
                loading: true,
                confirm:{msg:"是否删除选择的数据？"},
                okmsg: moduleName + '信息删除成功',
                cb: function (result) {
                    if (result.ok) {
                        self.reloadTableCurr();
                    }
                }
            });

        },

        delDataBatch: function(){
            var checkStatus = table.checkStatus('gwdc007_table');
            if (checkStatus.data.length > 0) {
                var _xdata = [];
                checkStatus.data.forEach(function (v) {
                    var _temp = app.fields_('GWDG_T');
                    _temp.GWDGENT = v.GWDGENT;
                    _temp.GWDGDOCNO = v.GWDGDOCNO;
                    _xdata.push(_temp);
                });
                app.ajax({
                    xurl: 'crm/gwdc007/delDataBatch',
                    xdata: _xdata,
                    loading: true,
                    toJsonStr: true,
                    confirm:{msg:"是否删除选择的数据？"},
                    okmsg: moduleName + '信息删除成功',
                    cb: function (result) {
                        if (result.ok) {
                            self.reloadTableCurr();
                        }
                    }
                });
            }
        },

        _ready: function () {
            this._super({
                vue: [{
                    el: '#page',
                    data: '',
                    searchId: 'gwdc007',
                    methods: {}
                }]
            });
            self.initTable();
        },
        _lazy: function () {
            this._super();
            layui.use(['table'], function () {
                element = layui.element;
            });
        }
    });

});